package com.ourcraft.informationcollectionsystem.service.province.formEdit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.DataAlterRecord;
import com.ourcraft.informationcollectionsystem.entity.DataReportForm;
import com.ourcraft.informationcollectionsystem.mapper.DataAlterRecordMapper;
import com.ourcraft.informationcollectionsystem.mapper.DataReportFormMapper;
import com.ourcraft.informationcollectionsystem.service.common.addQueryHistory.AddHistoryService;
import com.ourcraft.informationcollectionsystem.utils.statusCode.DataReportFormStatus;
import com.ourcraft.informationcollectionsystem.utils.statusCode.HistoryType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FormEditServiceImpl implements FormEditService{
    @Autowired
    private DataReportFormMapper dataReportFormMapper;

    @Autowired
    private DataAlterRecordMapper dataAlterRecordMapper;
    @Autowired
    private AddHistoryService addHistoryService;

    @Override
    public Boolean editEnterpriseData(DataReportForm dataReportForm, String historyComment) {
        var target = dataReportFormMapper.selectById(dataReportForm.getFormId());
        if (target.getStatus().ordinal() >= DataReportFormStatus.PASS.ordinal())
            return false;
        dataReportForm.setFormExamineStatus(DataReportFormStatus.PASS.ordinal());
        // 修改后记录修改前的原始数据以及修改行为本身
        dataAlterRecordMapper.insert(new DataAlterRecord(
                dataReportForm.getFormId(),
                target.toString(),
                null
        ));
        addHistoryService.addFormSubmission(
                dataReportForm.getFormId(),
                -1,
                HistoryType.PROVINCE_PASS.ordinal(),
                historyComment == null ? "省修改企业数据" : historyComment,
                false
        );
        return dataReportFormMapper.updateById(dataReportForm) != 0;
    }

    @Override
    public Boolean autoCompleteOneBlankEntry(Integer companyId, Integer formId) {
        var maxInvId = getCurrentInvId();
        return maxInvId != null && _autoCompleteOneBlankEntry(maxInvId, companyId, formId);
    }

    @Override
    public Boolean autoCompleteAllBlankEntries() {
        var maxInvId = getCurrentInvId();
        if (maxInvId == null)
            return false;

        var allEmptyFormQueryWrapper = new QueryWrapper<DataReportForm>()
                .eq("form_examine_status", DataReportFormStatus.NOT_FILL.ordinal())
                .select("company_id", "form_id");
        var allEmptyForms = dataReportFormMapper.selectList(allEmptyFormQueryWrapper);
        for(var form : allEmptyForms){
            _autoCompleteOneBlankEntry(maxInvId, form.getCompanyId(), form.getFormId());
        }
        return true;
    }

    private Integer getCurrentInvId(){
        var queryWrapper = new QueryWrapper<DataReportForm>()
                .select("max(investigation_id) as investigationId");
        return dataReportFormMapper.selectOne(queryWrapper).getInvestigationId();
    }

    private Boolean _autoCompleteOneBlankEntry(Integer maxInvId, Integer companyId, Integer formId){
        var originalForm = dataReportFormMapper.selectById(formId);
        // 原先的实现过于麻烦，直接使得end = start即可
        originalForm.setFormExamineStatus(DataReportFormStatus.PASS.ordinal());
        originalForm.setFormEndTotalNum(originalForm.getFormStartTotalNum());
        originalForm.setFormDecreaseType(null);
        originalForm.setFormFirstReason(null);
        originalForm.setFormSecondReason(null);
        originalForm.setFormThirdReason(null);
        originalForm.setFormReasonDescription(null);
        return editEnterpriseData(originalForm, "自动填充");
    }
}
